package com.chentongwei.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 日期工具类
 *
 * @author TongWei.Chen 2017-09-19 15:53:19
 */
public final class DateUtil {
    private DateUtil() {}

    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String TIME_FORMAT = "HH:mm:ss";
    private static final String YEAR_MOHTH_FORMAT = "yyyy-MM";
    private static Map<String, SimpleDateFormat> dateFormatMap = new ConcurrentHashMap<>();

    public static int hourDiff(Date date1, Date date2) throws ParseException {
        long from = date1.getTime();
        long to = date2.getTime();
        return (int) ((to - from)/(1000 * 60 * 60));
    }

    /**
     * 获取SimpleDateFormat
     *
     * @param parttern：日期格式
     * @return
     */
    private static SimpleDateFormat getDateFormat(String parttern) {
        SimpleDateFormat simpleDateFormat = dateFormatMap.get(parttern);
        if (simpleDateFormat != null) {
            return simpleDateFormat;
        } else {
            dateFormatMap.put(parttern, new SimpleDateFormat(parttern));
            return dateFormatMap.get(parttern);
        }
    }
}
